#include "global.h"
#include <QDir>

HTTPCliet                       g_httpClient;
rplyCenter_Dlg*                 g_rplyCenterDlg=nullptr;
srvCfg_Dlg*                     g_sysCfgDlg=nullptr;

const QString                   g_iniPath =QString("C:\\Users\\Public\\Documents\\DW\\rplyConfig.ini");
srvPara                         g_srvPara;

bool isFileExist(QString fullFilePath)
{
    QFileInfo fileInfo(fullFilePath);
    if(fileInfo.exists()){
        return true;
    }
    return false;
}

void readInitSettings()
{
    if(!isFileExist(g_iniPath)){
        g_srvPara.cvrPort=12000;
        g_srvPara.cvrIP.clear();
        g_srvPara.cvrUser.clear();
        g_srvPara.cvrPwd.clear();
        g_srvPara.cvrHttps = false;

        g_srvPara.platFormPort=8080;
        g_srvPara.platFormIP.clear();
        g_srvPara.platFormUser.clear();
        g_srvPara.platFormPwd.clear();
    }else{
        QSettings   *IniSettings;
        QStringList  keys;
        IniSettings =new QSettings(g_iniPath,QSettings::IniFormat);
        keys = IniSettings->allKeys();
        for(int i=0;i<keys.count();i++){
            QString sK =  keys[i];
            QString sV =  IniSettings->value(sK).toString();
            PRTLOG()<<sK<<sV;
            if(sK =="CVR/PORT"){
                g_srvPara.cvrPort=sV.toInt();
            }else if(sK =="CVR/IP"){
                g_srvPara.cvrIP=sV;
            }else if(sK =="CVR/UserName"){
                g_srvPara.cvrUser=sV;
            }else if(sK =="CVR/PWD"){
                g_srvPara.cvrPwd=sV;
            }else if(sK =="CVR/HTTPS"){
                if(sV.contains("enable",Qt::CaseInsensitive))
                    g_srvPara.cvrHttps=true;
                else
                    g_srvPara.cvrHttps=false;
            }else if(sK =="PLATFROM/PORT"){
                g_srvPara.platFormPort=sV.toInt();
            }else if(sK =="PLATFROM/IP"){
                g_srvPara.platFormIP=sV;
            }else if(sK =="PLATFROM/UserName"){
                g_srvPara.platFormUser=sV;
            }else if(sK =="PLATFROM/PWD"){
                g_srvPara.platFormPwd=sV;
            }
        }
    }
}

void changeSkin(){
    QFile t_styleSheetFile(":/skin/black.qss");
    t_styleSheetFile.open(QFile::ReadOnly);
    QString m_styleSheet = t_styleSheetFile.readAll();
    qApp->setStyleSheet(m_styleSheet);
}

void appExit(){
    if(g_sysCfgDlg != nullptr)g_sysCfgDlg->close();
    if(g_rplyCenterDlg != nullptr)g_rplyCenterDlg->close();
    qApp->exit();
}
